<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:h="http://java.sun.com/jsf/html"
			xmlns:f="http://java.sun.com/jsf/core">

  <h:body>

    <ui:composition template="./WEB-INF/view/template.xhtml">

      <ui:define name="contents">

        <h:panelGroup id="singleColumn" layout="block">

					<c:choose>
						<c:when test="${cartManager.cart.numberOfItems > 1}">
							<p>Your shopping cart contains ${cartManager.cart.numberOfItems} items.</p>
						</c:when>
						<c:when test="${cartManager.cart.numberOfItems == 1}">
							<p>Your shopping cart contains ${cartManager.cart.numberOfItems} item.</p>
						</c:when>
						<c:otherwise>
							<p>Your shopping cart is empty.</p>
						</c:otherwise>
					</c:choose>

					<h:panelGroup id="actionBar" layout="block">

						<!--clear cart widget -->
						<c:if test="${!empty cartManager.cart and
													cartManager.cart.numberOfItems != 0}">
							<h:link outcome="index"  styleClass="bubble hMargin"
											value="clear cart">
                <f:param name="clear" value="true"/>
							</h:link>
						</c:if>

						<!-- continue shopping widget -->
						<c:choose>
							<!-- if 'selectedCategory' session object exists, send user to previously viewed category -->
							<c:when test="${!empty selectedCategory}">
								<h:link outcome="category"  styleClass="bubble hMargin"
												value="continue shopping">
									<f:param name="cid" value="${selectedCategory.id}"/>
								</h:link>
							</c:when>
							<!-- otherwise send user to welcome page -->
							<c:otherwise>
								<h:link outcome="index"  styleClass="bubble hMargin"
												value="continue shopping">
								</h:link>
							</c:otherwise>
						</c:choose>

						<!-- checkout widget -->
						<c:if test="${!empty cartManager.cart and
													cartManager.cart.numberOfItems != 0}">
							<h:link outcome="checkout"  styleClass="bubble hMargin"
											value="proceed to checkout &#x279f;">
							</h:link>
						</c:if>

					</h:panelGroup>

					<c:if test="${!empty cartManager.cart and
												cartManager.cart.numberOfItems != 0}">

						<h4 id="subtotal">subtotal: &euro; ${cartManager.cart.subtotal}</h4>

						<table id="cartTable">

							<tr class="header">
								<th>product</th>
								<th>name</th>
								<th>price</th>
								<th>quantity</th>
							</tr>

							<c:forEach var="cartItem" items="${cartManager.cart.items}" varStatus="iter">

								<c:set var="product" value="${cartItem.product}"/>

								<tr class="${((iter.index % 2) == 0) ? 'lightBlue' : 'white'}">
									<td>
										<img src="${initParam.productImagePath}${product.name}.png"
												 alt="${product.name}"/>
									</td>

									<td>${product.name}</td>

									<td>
										&euro; ${cartItem.total}
										<br/>
										<span class="smallText">( &euro; ${product.price} / unit )</span>
									</td>

									<td>

										<form action="#{cartManager.updateProduct()}" method="post">
											<input type="hidden"
														 name="productId"
														 value="#{product.id}"/>
											<input type="text"
														 maxlength="2"
														 size="2"
														 value="#{cartItem.quantity}"
														 name="quantity"
														 style="margin:5px"/>
											<input type="submit"
														 name="submit"
														 value="update"/>
										</form>

									</td>

								</tr>

							</c:forEach>

						</table>

					</c:if>
				</h:panelGroup>

			</ui:define>

		</ui:composition>

	</h:body>

</html>
